#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            board[i][j] = ' ';
        }
    }
}
/* void DisplayBoard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    for (i = 0; i < row; i++)
    {
        // 打印数据
        printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]); // 棋盘写死了；改用循环
        // 打印分割信息
        if (i < row - 1)
            printf("---|---|---\n");
    }
} */
void DisplayBoard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    for (i = 0; i < row; i++)
    {
        // 打印数据
        // printf(" %c | %c | %c |\n", board[i][0], board[i][1], board[i][2]); // 棋盘写死了；改用循环
        int j = 0;
        for (j = 0; j < col; j++)
        {
            printf(" %c ", board[i][j]);
            if (j < col - 1)
                printf("|");
        }
        printf("\n");
        // 打印分割信息
        if (i < row - 1)
        {
            int j = 0;
            for (j = 0; j < col; j++)
            {
                printf("---");
                if (j < col - 1)
                {
                    printf("|");
                }
            }
            printf("\n");
        }
    }
}

void PlayerMove(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    printf("Player playing chess\n");
    while (1)
    {
        printf("Please enter coordinates (e.g. 1 2), separated by spaces)\n-->");
        scanf("%d %d", &x, &y);
        // 判断坐标位置是否合法
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*';
                break;
            }
        }
        else
        {
            printf("\n Illegal coordinates");
        }
    }
}

void ComputerMove(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    while (1)
    {
        printf("Computer chess\n");
        // printf("电脑下棋\n");

        x = rand() % row; // 0-2
        y = rand() % col; // 0-2
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            break;
        }
    }
}
// 判断输赢
// 满了返回1    没满返回0
int IsFull(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (board[i][j] == ' ')
            {
                return 0;
            }
        }
    }

    return 1;
}

char IsWin(char board[ROW][COL], int row, int col)
{
    int i = 0;
    // 判断行  赢
    for (i = 0; i < row; i++)
    {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
        {
            return board[i][1];
        }
    }
    // 判断列赢
    int j = 0;
    for (j = 0; j < ROW; j++)
    {
        if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
        {
            return board[1][j];
        }
    }
    // 判断对角线赢
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
    {
        return board[1][1];
    }
    if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
    {
        return board[1][1];
    }
    if (IsFull(board, row, col))
    {
        return 'q'; // c退出
    }
    return 'c';
}
